package com.nb.ds.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONUtil;
import com.nb.ds.datasource.DS;
import com.nb.ds.datasource.DsConstant;
import com.nb.ds.entity.Student;
import com.nb.ds.entity.User;
import com.nb.ds.mapper.UserMapper;
import com.nb.ds.service.IStudentService;
import com.nb.ds.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @author lihaoyang6
 * @date 2024年04月22日 15:24
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    IStudentService studentService;



    @DS(DsConstant.ds1)
    @Override
    public List<User> findAll() {
        return userMapper.selectAll();
    }

    @Transactional
    @DS(DsConstant.ds1)
    @Override
    public Map<String,Object> findTwo() {
        List<User> users = findAll();
        System.err.println(JSONUtil.toJsonStr(users));
        List<Student> students = studentService.findAll();
        System.err.println(JSONUtil.toJsonStr(students));

        Map<String,Object> map = new HashMap<>();
        map.put("users",users);
        map.put("students",students);
        return map;
    }






    //    @DS(DsConstant.ds2)
//    @Override
//    public List<User> findAll2() {
//        return userMapper.selectAll();
//    }
//
//
//    @Transactional(transactionManager = "transactionManager2")
//    @DS(DsConstant.ds2)
//    @Override
//    public void addByDs2() {
//        User user = new User();
//        user.setName("张三-"+ RandomUtil.randomInt());
//        user.setAge(RandomUtil.randomInt());
//        user.setSex("男");
//        userMapper.insertUser2(user);
//        int k=1/0;
//    }
//
//    @Transactional(transactionManager = "transactionManager1")
//    @DS(DsConstant.ds1)
//    @Override
//    public void addByDs1() {
//        User user = new User();
//        user.setName("XIAOHONG-"+ RandomUtil.randomInt());
//        user.setAge(RandomUtil.randomInt());
//        user.setSex("nv");
//        userMapper.insertUser2(user);
//    }
//
//    /**
//     *
//     */
//
    @DS(DsConstant.ds1)
    @Transactional
    @Override
    public void addAll() {
        //事务失效，ds1插入成功，ds2无数据
        User user = new User();
        user.setName("XIAOHONG-"+ RandomUtil.randomInt());
        user.setAge(RandomUtil.randomInt());
        user.setSex("nv");
        userMapper.insertUser2(user);

        studentService.insert();
    }

    @Transactional
    @DS(DsConstant.ds1)
    @Override
    public void addUserTs() {
        User user = new User();
        user.setName("XIAOHONG-"+ RandomUtil.randomInt());
        user.setAge(RandomUtil.randomInt());
        user.setSex("nv");
        userMapper.insertUser2(user);

        User user2 = new User();
        user2.setName("XIAOHONG-"+ RandomUtil.randomInt());
        user2.setAge(RandomUtil.randomInt());
        user2.setSex("nv");
        userMapper.insertUser2(user2);
        throw new RuntimeException("Error~~~~~~~~~~");
    }


    //
//
////    @Autowired
////    TransactionTemplate transactionTemplate1;
////    @Autowired
////    TransactionTemplate transactionTemplate2;
//
//    @Override
//    public void addAll2() {
////        transactionTemplate1.execute((ds1Status) -> {
////            transactionTemplate2.execute((ds2Status) -> {
////                try {
////                    addByDs1();
////                    addByDs2();
////                    int a = 1 / 0;
////                } catch (Exception e) {
////                    e.printStackTrace();
////                    ds1Status.setRollbackOnly();
////                    ds1Status.setRollbackOnly();
////                    return false;
////                }
////                return true;
////            });
////            return true;
////        });
//    }
//
//
//
//    @DS(DsConstant.ds1)
////    @Transactional(transactionManager = "transactionManager1")
//    @Override
//    public void addByMain() {
//        User user = new User();
//        user.setName("张三-"+ RandomUtil.randomInt());
//        user.setAge(RandomUtil.randomInt());
//        user.setSex("男");
//        userMapper.insertUser2(user);
//        addBySub();
//    }
//
//    @DS(DsConstant.ds2)
////    @Transactional(transactionManager = "transactionManager2",propagation = Propagation.NOT_SUPPORTED)
//    @Override
//    public void addBySub() {
//        User user = new User();
//        user.setName("张三-"+ RandomUtil.randomInt());
//        user.setAge(RandomUtil.randomInt());
//        user.setSex("男");
//        userMapper.insertUser2(user);
//        int k=1/0;
//    }
}
